<?php
namespace app\index\controller;

use app\index\Controller;
use think\Db;
class End extends Controller{
	/*
	 *活动结束的当天晚上24点，自动结束活动 
	 * 活动结束时间小于当前时间戳 && is_end=0
	 * */
	public function end(){
		$map['end_time'] = ['<',time()];
		$map['is_end'] = 0;
		$info = Db::table('tp_info')->field('id,user_id,pay_type')->where($map)->select();
		foreach($info as $k => $v){
			/*
			 *将没有点击活动结束的人自动结束
			 *将info表的满足条件的活动is_end=1
			 *将活动发起人的押金返还给他
			 *给活动发起人发放积分
			 */
			$money = $v['max_people']*$v['deposit'];//发起人所交押金
			$offered_map['is_end'] = 1;
			$offered_map['button_status'] = 2;
			$res = Db::table('tp_offered')->where('active_id',$v['id'])->update($offered_map);//将没有点击结束活动的用户自动结束
			if($res){
				if($v['pay_type'] == 0){
					//AA制
					Db::table('tp_info')->where('id',$v['id'])->setField('is_end',1);
					Db::table('tp_user')->where('id',$v['user_id'])->setInc('money',$money);
					Db::table('tp_user')->where('id',$v['user_id'])->setInc('Credit',CREDIT);
				}else{
					//我来买单（发放双倍积分）
					Db::table('tp_info')->where('id',$v['id'])->setField('is_end',1);
					Db::table('tp_user')->where('id',$v['user_id'])->setField('Credit',CREDIT+CREDIT);
				}
				$info = Db::table('tp_user')->where('id',$v['user_id'])->find();
				if($info['Credit']>100){
					$credit = 100;
				}
			}
		}

        //每天凌晨将签到相关信息清空
        $user_sql = "update tp_user set is_qiandao = 0";
        Db::execute($user_sql);
	}
}
?>